<?php

class User extends BaseActiveRecord
{
    public static $aCustomerTypes = array
    (
        'legal_entity'        => 'Юридическое лицо',
        'individual'          => 'Физическое лицо',
        'sole_proprietorship' => 'Индивидуальный предприниматель'
    );
    
    public $login;
    public $password;
    public $remember = false;

    public function tableName()
    {
        return '{{users}}';
    }

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function relations()
    {
        return array
        (
            'address'            => array(self::BELONGS_TO, 'Address', 'address_id'),
            'profile'            => array(self::BELONGS_TO, 'Profile', 'profile_id'),
            'profile_legal_info' => array(self::BELONGS_TO, 'ProfileLegalInfo', 'profile_legal_info_id'),
            'tariff_plans'       => array(self::BELONGS_TO, 'TariffPlan', '', 'on' => 't.user_id = tariff_plans.user_id')
        );
    }

    public function rules()
    {
        return array
        (
            array('login', 'required', 'message' => 'Заполните поле <b>Логин</b>.',     'on' => 'login'),
            array('password', 'required', 'message' => 'Заполните поле <b>Пароль</b>.', 'on' => 'login'),
            array('remember', 'boolean'),
            array('password', 'authenticate', 'on' => 'login')
        );
    }
    
    public function attributeLabels() 
    {
        return array
        (
            'user_login'    => 'Логин',
            'user_password' => 'Пароль',
            'status'        => 'Статус',
            'user_cdate'    => 'Дата регистрации'
        );
    }

    public function authenticate($attribute, $params)
    {
        $oIdentity = new UserIdentity($this->login, $this->password);
        
        if( !$oIdentity->authenticate() )
        {
            $this->addError('password','Неверный логин или пароль.');
        }
        else
        {
            if ( $this->remember )
            {
                Yii::app()->user->login($oIdentity, 3600*24*7);
            }
            else
            {
                Yii::app()->user->login($oIdentity);
            }
        }
    }

    public function  checkboxes()
    {
        return array
        (
            'remember'
        );
    }
    
    public static function getUsersDataProvider($sStatus)
    {
        if (!is_array($sStatus))
        {
            $sStatus = array($sStatus);
        }

        $oCriteria            = new CDbCriteria();
        $oCriteria->condition = 't.status IN ("'.implode('","', $sStatus).'") AND t.address_id > 0 AND t.profile_legal_info_id > 0';
        $oCriteria->order     = 't.user_cdate DESC';
        
        if (in_array('pending', $sStatus))
        {
            //$oCriteria->select    = 't.*, tariff_plans.*';
            //$oCriteria->join = 'INNER JOIN tariff_plans ON (tariff_plans.user_id = t.user_id AND tariff_plans.is_first = 1)';
            $oCriteria->with = array('tariff_plans' =>
                            array(
                                'select' => 'name',
                                'condition' => 'tariff_plans.is_first = 1',
                                'joinType'=>'INNER JOIN',
                            ));
            
        }
        
        $oDataProvider = new CActiveDataProvider('User', array
        (
            'criteria' => $oCriteria,
            'pagination' => array
            (
                'pageSize' => 100
            )
        ));
        
        return $oDataProvider;
    }

    public static function getUsersMetalsDataProvider()
    {
        $oCriteria            = new CDbCriteria();
        $oCriteria->join      = 'INNER JOIN auth_assignment ON (auth_assignment.userid = user_id AND auth_assignment.itemname = "user_sole_proprietorship")';
        $oCriteria->condition = 'status IN ("active", "inactive")';
        $oCriteria->order     = 'user_cdate DESC';

        $oDataProvider = new CActiveDataProvider('User', array
        (
            'criteria' => $oCriteria,
            'pagination' => array
            (
                'pageSize' => 100
            )
        ));

        return $oDataProvider;
    }
}